package com.shuoyue.ycgk.ui.course.contract;

import com.shuoyue.ycgk.base.BaseModel;
import com.shuoyue.ycgk.base.BasePresenter;
import com.shuoyue.ycgk.base.BaseResult;
import com.shuoyue.ycgk.base.BaseView;
import com.shuoyue.ycgk.base.ListWithPage;
import com.shuoyue.ycgk.entity.Course;
import com.shuoyue.ycgk.entity.CourseInfo;
import com.shuoyue.ycgk.entity.TimerTable;
import com.shuoyue.ycgk.net.RetrofitClient;
import com.shuoyue.ycgk.net.base.ApiSubscriber;
import com.shuoyue.ycgk.net.exception.NetErrorException;
import com.shuoyue.ycgk.net.gsondefaultadapter.Optional;

import io.reactivex.Observable;

public interface CourseContract {
    interface CourseListView extends BaseView {
        void setCourseList(ListWithPage<Course> list);

        void setCourseInfo(CourseInfo info);

        void setVideoInfo(TimerTable timerTable,TimerTable adapterData);

        void notBuyNotice();
    }


    class Presenter extends BasePresenter<CourseListView> {
        Model model = new Model();

        public void getCourseList(Integer moduleId, int subjectId, int page) {
            apply(model.getCourseList(moduleId, subjectId, page))
                    .subscribe(new ApiSubscriber<Optional<ListWithPage<Course>>>(mView, this) {
                        @Override
                        public void onNext(Optional<ListWithPage<Course>> listWithPageOptional) {
                            super.onNext(listWithPageOptional);
                            mView.setCourseList(listWithPageOptional.getIncludeNull());
                        }
                    });
        }

        public void getCourseInfo(int id) {
            apply(model.getCourseInfo(id))
                    .subscribe(new ApiSubscriber<Optional<CourseInfo>>(mView, this) {
                        @Override
                        public void onNext(Optional<CourseInfo> listWithPageOptional) {
                            super.onNext(listWithPageOptional);
                            mView.setCourseInfo(listWithPageOptional.getIncludeNull());
                        }
                    });
        }

        public void getVideoInfo(TimerTable id) {
            apply(model.getVideoInfo(id.getId()))
                    .subscribe(new ApiSubscriber<Optional<TimerTable>>(mView, this) {
                        @Override
                        public void onNext(Optional<TimerTable> listWithPageOptional) {
                            super.onNext(listWithPageOptional);
                            mView.setVideoInfo(listWithPageOptional.getIncludeNull(),id);
                        }

                        @Override
                        protected void onFail(NetErrorException error) {

                            if(error.getErrorType()==1) {
                                mView.notBuyNotice();
                            }else {
                                super.onFail(error);
                            }
                        }
                    });
        }
    }

    class Model extends BaseModel {
        Observable<BaseResult<ListWithPage<Course>>> getCourseList(Integer moduleId, Integer subjectId, int page) {
            return RetrofitClient.getInstance().getApi().getCourseList(moduleId, subjectId, page);
        }

        Observable<BaseResult<CourseInfo>> getCourseInfo(Integer id) {
            return RetrofitClient.getInstance().getApi().getCourseInfo(id);
        }

        Observable<BaseResult<TimerTable>> getVideoInfo(Integer id) {
            return RetrofitClient.getInstance().getApi().getVideoInfo(id);
        }
    }
}
